<?php
namespace Tongcheng\Controller;
use Think\Controller;
class HouseController extends CommonController {
    public function index(){
        if(IS_AJAX){
            if($_GET['type'] == 4){
                $model = M("HouseMember");

                $map['wid'] = session('wid');

                $res['count'] = $model->where($map)->count();
                $page = $_GET['page']?$_GET['page']:1;
                $limit = $_GET['limit']?$_GET['limit']:10;
                $res['list'] = $model->where($map)->order("last_time desc")->page("{$page},{$limit}")->select();
            }else{
                $model = M("House");

                $map['wid'] = session('wid');
                $map['type'] = $_GET['type'];
                $map['status'] = 1;

                $res['count'] = $model->where($map)->count();
                $page = $_GET['page']?$_GET['page']:1;
                $limit = $_GET['limit']?$_GET['limit']:10;
                $res['list'] = $model->where($map)->order("last_time desc")->page("{$page},{$limit}")->select();
            }

            $this->ajaxReturn($res);
        }else{
            $this->display();
        }
    }

    // public function add(){
    // 	$model = M("InfoCate");
    // 	$map['wid'] = session('wid');
    // }

    // public function edit(){
    // 	$model = M("InfoCate");
    // 	$map['wid'] = session('wid');
    // }
    public function top(){
        $model = M("House");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];

        $new['is_top'] = $_POST['status'];
        $new['top_end_time'] = date("Y-m-d H:i:s", strtotime("2099-12-23 00:00:00"));
        if($model->where($map)->save($new)){
            $eq['wid'] = session('wid');
            $eq['action'] = "house";
            $eq['info_id'] = $_POST['id'];
            M("InfoOrder")->where($eq)->save($new);
            $this->success("置顶成功！");
        }else{
            $this->error("网络忙，请稍后再试！".$model->getLastSql());
        }
    }

    public function tui(){
        $model = M("House");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];

        $new['is_tui'] = $_POST['status'];
        if($model->where($map)->save($new)){
            $this->success("推荐成功！");
        }else{
            $this->error("网络忙，请稍后再试！");
        }
    }

    public function zj_top(){
        $model = M("HouseMember");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];

        $new['is_top'] = $_POST['status'];
        if($model->where($map)->save($new)){
            $this->success("置顶成功！");
        }else{
            $this->error("网络忙，请稍后再试！".$model->getLastSql());
        }
    }

    public function zj_tong(){
        $model = M("HouseMember");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];

        $new['status'] = $_POST['status'];
        if($model->where($map)->save($new)){
            $this->success("设置成功！");
        }else{
            $this->error("网络忙，请稍后再试！");
        }
    }

    public function zj_del(){
        $model = M("HouseMember");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];

        if($model->where($map)->delete()){
            $this->success("删除成功！");
        }else{
            $this->error("网络忙，请稍后再试！");
        }
    }

    public function del(){
        $model = M("House");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];

        if($model->where($map)->delete()){
            $this->success("删除成功！");
        }else{
            $this->error("网络忙，请稍后再试！");
        }
    }
}